package com.simp.controller;

import com.simp.aop.SysLog;
import com.simp.dto.SysDictDto;
import com.simp.entity.SysDictEntity;
import com.simp.model.SysDictModel;
import com.simp.page.PageVO;
import com.simp.page.R;
import com.simp.page.SuperController;
import com.simp.service.SysDictService;
import com.simp.utils.BeanConvertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author pan.shun
 * @since 2022/3/14 17:01
 */
@Api(tags = "系统字典")
@RestController
@RequestMapping("/dict")
public class SysDictController extends SuperController {

    @Autowired
    private SysDictService sysDictService;

    @ApiOperation(value = "字典查询列表", notes = "字典分页查询列表，条件为 > code=&name= ,其中code 为字典编码，name为字典名称 ", httpMethod = "GET")
    @GetMapping("/list")
    public R<PageVO<SysDictEntity>> getPageList(
            SysDictDto sysDictDto) {
        PageVO<SysDictEntity> sysDictPageVO = sysDictService.queryDictPageList(sysDictDto);
        return R.success(sysDictPageVO);
    }

    @SysLog(value = "系统字典", addOrUpdate = true)
    @ApiOperation(value = "字典新增或修改", httpMethod = "POST")
    @PostMapping("/addOrUpdate")
    public R addOrUpdate(@RequestBody SysDictModel sysDict) {
        SysDictEntity sysDictEntityEntity = BeanConvertUtils.convert(sysDict, SysDictEntity.class);
        sysDictService.addOrUpdate(sysDictEntityEntity);
        return success();
    }

    @SysLog(value = "系统字典删除")
    @ApiOperation(value = "字典删除", notes = "通过ID删除字典", httpMethod = "GET")
    @GetMapping("/removeById/{ids}")
    public R removeById(@ApiParam(name = "id", required = true, value = "字典ID") @PathVariable String ids) {
        sysDictService.removeDictById(ids);
        return success();
    }

    @ApiOperation(value = "通过字典编码查询其子集", notes = "通过字典编码查询其子集", httpMethod = "GET")
    @GetMapping("/getChildrenDictByCode/{code}")
    public R getChildrenDict(@ApiParam(name = "code", value = "字典编码", required = true) @PathVariable String code) {
        return sysDictService.getChildrenDictByCode(code);
    }

    @ApiOperation(value = "通过字典ID查询其子集", notes = "通过字典ID查询其子集", httpMethod = "GET")
    @GetMapping("/getChildrenDictById/{id}")
    public R getChildrenDictById(@ApiParam(name = "id", value = "字典父级ID", required = true) @PathVariable String id) {
        return sysDictService.getChildrenDictById(id);
    }
}
